﻿using System;
using System.Linq;
using System.Web.Mvc;
using Matrix.MaKaYunShang.BLL;

namespace Matrix.MaKaYunShang.MagWeb.Controllers
{
    [Authorization]
    [Matrix.MaKaYunShang.Model.Attributes.Skip]
    public class UserCardController : BaseController
    {
        #region 列表

        public ActionResult List()
        {
            return View();
        }

        [HttpPost]
        [Matrix.MaKaYunShang.Model.Attributes.Skip]
        public JsonResult Listing(int draw, int start, int length, string startTime, string endTime, string keyWord, string shopName, int cateId = 0, int status = -1)
        {
            try
            {
                int order;
                int.TryParse(Request["order[0][column]"], out order);
                var direction = Request["order[0][dir]"];
                var orderByColumns = new[] { "CloudCardId", "CloudCardId", "CardCode", "CardStatus", "AddTime", "VerifyTime", "ReceiveUserId", "ShopId", "VerifyShopId" };
                var shopIds = string.Empty;
                if (!string.IsNullOrEmpty(shopName))
                {
                    var shopList = ShopsBLL.Instance.GetListByName(shopName);
                    shopIds = shopList.Count > 0 ? string.Join(",", shopList.Select(l => l.Id)) : "-1";
                }
                var pageIndex = (start / length) + 1;
                var theList = UserCardBLL.Instance.GetPagingList(draw, pageIndex, length, keyWord, orderByColumns[order] + " " + direction, status, shopIds, false, startTime, endTime, cateId);
                return Json(theList);
            }
            catch (Exception ex)
            {
                Logger.Error("UserCard Listing Catch Error: " + ex.Message);
                return Json(false);
            }
        }

        #endregion
    }
}